
#include <iostream>
#include "RenderableEntity.h"
#include "Painter.h"
#include "TextureManager.h"
#include "InputManager.h"
#include "defs.h"

RenderableEntity::RenderableEntity()
{
    setup(NULL);
}

RenderableEntity::RenderableEntity(const Texture* _tTexture):Entity::Entity()
{
    setup(_tTexture);
}

RenderableEntity::RenderableEntity(const Texture* _tTexture, Vector2 _vPosition):Entity::Entity(_vPosition)
{
    setup(_tTexture);
}

RenderableEntity::RenderableEntity( std::string _sName, Vector2 _vPosition):Entity::Entity(_vPosition)
{
    entityName = _sName;
    setup((Texture*) TextureManager::instance().getTextureByName(_sName));
}

void RenderableEntity::setup(const Texture* _tTexture)
{
    m_tTexture = (Texture*)_tTexture;
    
    m_cColor = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
    m_bFlipHorizontal = false;
    m_bFlipVertical = false;
    
    if(m_tTexture)
        m_vSize = m_tTexture->getSize(); 
    
    m_vButtonOffset = Vector2(0,0);
    m_vButtonSize = m_vSize;
    
    m_bPressed = false;
    
    m_vStartUV.x = 0.0f;
    m_vStartUV.y = 0.0f;
    m_vEndUV.x = 1.0f;
    m_vEndUV.y = 1.0f;
    
    m_bChangeColorWhenClicked = false;
}

RenderableEntity::~RenderableEntity()
{
}

void RenderableEntity::update()
{
    Entity::update();
    
    if(m_bChangeColorWhenClicked)
    {
        bool bPressed = isPressed();
        if(bPressed && !m_bPressed)
        {
            m_cNormalColor = m_cColor;
            m_cColor = m_cClickedColor;
            m_bPressed = bPressed;
        }
        else if(!bPressed && m_bPressed)
        {
            m_cColor = m_cNormalColor;
            m_bPressed = bPressed;
        }
    }
}

void RenderableEntity::renderShadow(Vector4 _vColor, Vector2 _vOffset)
{
    float fAlpha = getAlpha();
    Vector4 cColor = getColor();

    setColor(_vColor);
    setAlpha(fAlpha);
    
    render(_vOffset);
    
    setColor(cColor);
    //setAlpha(fAlpha);
}

void RenderableEntity::renderShadow(Vector2 _vOffset)
{
    renderShadow(COLOR_BLACK, _vOffset);
}

void RenderableEntity::render()
{
    render(Vector2(0.0f, 0.0f));
}

void RenderableEntity::render(Vector2 _vOffset)
{
    if (!m_bRender) return;
    
    Vector2 size = m_vSize;
    
    size.x *= m_vScale.x;
    size.y *= m_vScale.y;
    
    // update the position offset of the UVs, the offset is in units
    //Vector2 vOffsetPos = Vector2((m_vStartUV.x + 1 - m_vEndUV.x) * size.x, m_vStartUV.y + 1 - m_vEndUV.y);
    //vOffsetPos *= 0.5f;
    
    // the new size of the quad, because we are rendering only a part of the texture
    size.x *=  (m_vEndUV.x - m_vStartUV.x);
    size.y *=  (m_vEndUV.y - m_vStartUV.y);

    if(m_bFlipHorizontal || m_bFlipVertical)
    {
        Vector2 startUV = Vector2(m_bFlipHorizontal ? 1.0f - m_vStartUV.x : m_vStartUV.x, m_bFlipVertical ? 1.0f - m_vStartUV.y : m_vStartUV.y);
        Vector2 endUV = Vector2(m_bFlipHorizontal ? 1.0f - m_vEndUV.x : m_vEndUV.x, m_bFlipVertical ? 1.0f - m_vEndUV.y : m_vEndUV.y);
        
        Painter::drawTexturedQad(m_vPosition.x + _vOffset.x, m_vPosition.y + _vOffset.y, m_vCenter.x, m_vCenter.y, size.x, size.y, startUV.x, startUV.y, endUV.x, endUV.y, m_fRotation, m_tTexture, m_cColor);
    }
    else
    {
        Painter::drawTexturedQad(m_vPosition.x + _vOffset.x, m_vPosition.y + _vOffset.y, m_vCenter.x, m_vCenter.y, size.x, size.y, m_vStartUV.x, m_vStartUV.y, m_vEndUV.x, m_vEndUV.y, m_fRotation, m_tTexture, m_cColor);
    }
}

//Button
void RenderableEntity::setupButton(Vector2 _vOffset, Vector2 _vSize)
{
    m_vButtonOffset = _vOffset;
    m_vButtonSize = _vSize;
}

void RenderableEntity::setupButton(Vector2 _vButtonScale)
{
    m_vButtonSize = getSize() * _vButtonScale;
    m_vButtonOffset = (getSize() - m_vButtonSize) * 0.5f;
}

bool RenderableEntity::isClicked()
{
    bool bReleased = InputManager::instance().hasJustReleased();
    if(bReleased)
    {
        Vector2 vPos = m_vPosition + m_vButtonOffset - m_vSize * m_vCenter * m_vScale;
        Vector2 vSize = m_vButtonSize * m_vScale;
        
        return InputManager::instance().positionInRect(vPos, vSize);
    }
    return false;
}

bool RenderableEntity::isPressed()
{
    return InputManager::instance().isPressed() && InputManager::instance().positionInRect(m_vPosition + m_vButtonOffset - m_vSize * m_vCenter * m_vScale, m_vButtonSize * m_vScale);
}

void RenderableEntity::changeColorWhenClicked(bool _bChangeColor, Vector4 _cColor)
{
    m_bChangeColorWhenClicked = _bChangeColor;
    m_cClickedColor = _cColor;
    m_cNormalColor = m_cColor;
}
